package com.yakumosakura.yakumoblade.entity.hexgram.b;

import com.yakumosakura.yakumoblade.entity.exer.absNeoSummonSword;
import com.yakumosakura.yakumoblade.entity.hexgram.a.HexGramSumonSwordEntity;
import com.yakumosakura.yakumoblade.registry.slashblade.YAEntitiesRegistry;
import com.yakumosakura.yakumoblade.utils.SlashBladeUtil;
import mods.flammpfeil.slashblade.entity.IShootable;
import mods.flammpfeil.slashblade.entity.Projectile;
import mods.flammpfeil.slashblade.item.ItemSlashBlade;
import mods.flammpfeil.slashblade.util.RayTraceHelper;
import mods.flammpfeil.slashblade.util.TargetSelector;
import net.minecraft.network.syncher.EntityDataAccessor;
import net.minecraft.network.syncher.EntityDataSerializers;
import net.minecraft.network.syncher.SynchedEntityData;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.sounds.SoundEvents;
import net.minecraft.sounds.SoundSource;
import net.minecraft.world.damagesource.DamageSource;
import net.minecraft.world.damagesource.DamageTypes;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.EntityType;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.level.ClipContext;
import net.minecraft.world.level.Level;
import net.minecraft.world.phys.BlockHitResult;
import net.minecraft.world.phys.EntityHitResult;
import net.minecraft.world.phys.HitResult;
import net.minecraft.world.phys.Vec3;
import net.minecraftforge.network.PlayMessages;
import org.joml.Vector3f;

import java.util.List;
import java.util.Optional;
import java.util.stream.Stream;

public class SwordRainEntityDragon extends absNeoSummonSword {
    public SwordRainEntityDragon(EntityType<? extends Projectile> entityTypeIn, Level worldIn) {
        super(entityTypeIn, worldIn);
    }
    public static SwordRainEntityDragon createInstance(PlayMessages.SpawnEntity packet, Level worldIn) {
        return new SwordRainEntityDragon(YAEntitiesRegistry.swordRainEntityDragon, worldIn);
    }
    @Override
    public void tick() {
        this.setYRot(-90);
        this.yRotO = -90;
        super.tick();
    }

    @Override
    protected void onHitBlock(BlockHitResult blockraytraceresult) {
        super.onHitBlock(blockraytraceresult);
        if (this.tickCount>10) {
            var damageSource = new DamageSource(this.level().registryAccess()
                    .registryOrThrow(net.minecraft.core.registries.Registries.DAMAGE_TYPE)
                    .getHolderOrThrow(DamageTypes.MAGIC), this.getOwner());
          this.level().getEntitiesOfClass(
                    LivingEntity.class,
                    this.getBoundingBox().inflate(5f)).forEach(livingEntity -> {
                if (livingEntity != this.getOwner()) {
                    livingEntity.invulnerableTime=0;
                    livingEntity.hurt(damageSource, (float) (getDamage()*2f));
                }
            });

            this.discard();
        }
    }

    @Override
    protected void onHitEntity(EntityHitResult entityHitResult) {
        super.onHitEntity(entityHitResult);

        this.discard();

    }
}
